package com.hmall.gateway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author chanchaw
 * @create 2025-08-02 15:49
 */
@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        HttpHeaders headers = request.getHeaders();
        System.out.println("headers:" + headers.toString());
        return chain.filter(exchange);// 放行
    }

    @Override
    public int getOrder() {
        // 最后一个过滤器的序号是 int 的最大值，这里设置大于其即可
        // 保证在最后一个过滤器之前执行自定义的逻辑来控制是否继续
        return 0;
    }
}
